# _*_ coding: utf-8 _*_
from odoo import models, fields, api, _


class MrpProduction(models.Model):
    _inherit = "mrp.production"

    track_number = fields.Char(u"追踪号")

    @api.model
    def create(self, values):
        if not values.get('name', False) or values['name'] == _('New'):
            if values.get('picking_type_id'):
                values['name'] = self.env['stock.picking.type'].browse(
                    values['picking_type_id']).sequence_id.next_by_id()
            else:
                values['name'] = self.env['ir.sequence'].next_by_code('mrp.production') or _('New')
        if not values.get('procurement_group_id'):
            procurement_group = self.env["procurement.group"].create({
                'name': values['name'],
                "track_number": values.get("track_number")
            })
            values['procurement_group_id'] = procurement_group.id

        procurement_group_id = values.get("procurement_group_id")
        if procurement_group_id:
            procurement_group = self.env["procurement.group"].browse(procurement_group_id)
            values.setdefault("track_number", procurement_group.track_number)
        production = super(MrpProduction, self).create(values)

        return production
